package com.tanhua.server.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private MovementsService movementsService;


    //发布动态
    @PostMapping
    public ResponseEntity<Object> movements(Movement movement, MultipartFile imageContent[]) throws IOException {
        movementsService.publishMovement(movement, imageContent);
        return ResponseEntity.ok("ok");
    }

    //分页查询：我的动态
    @GetMapping("/all")
    public ResponseEntity<Object> findMyMovementsByUserId(Long userId, @RequestParam(defaultValue = "1") Integer page,
                                                          @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementsService.findMyMovementsByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    //查询好友动态

    @GetMapping
    public ResponseEntity<Object> movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        // 调用service层查询
        PageResult pr = movementsService.findFriendMovements(page, pagesize);
        // 返回结果
        return ResponseEntity.ok(pr);
    }


    //查询推荐动态
    @GetMapping("/recommend")
    public ResponseEntity<Object> recommend(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult pr = movementsService.findRecommendMovements(page, pageSize);
        return ResponseEntity.ok(pr);
    }

    //查询单条动态
    @GetMapping("/{id}")
    public ResponseEntity<Object> findMovementById(@PathVariable("id") String movementId) {
        MovementsVo vo = movementsService.findMovementById(movementId);
        return ResponseEntity.ok(vo);
    }

    // 点赞
    @GetMapping("/{id}/like")
    public ResponseEntity<Object> like(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.likeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    //取消点赞
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Object> dislike(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.dislikeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    //喜欢

    @GetMapping("/{id}/love")
    public ResponseEntity<Object> love(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.loveComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    //取消喜欢
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Object> unlove(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.disloveComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    //谁看过我
    @GetMapping("visitors")
    public ResponseEntity<Object> queryVisitorsList(){
        List<VisitorsVo> list = movementsService.queryVisitorsList();
        return ResponseEntity.ok(list);
    }
}